﻿using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;
using WepApi07.Data;
using WepApi07.Data.Entity;
using WepApi07.Domain;
using System.Linq;

namespace WepApi07.Util
{
    public class DbInitializazer
    {
        public static void Seed(IServiceProvider serviceProvider)
        {

            using(var scope = serviceProvider.CreateScope())
            {
                var db = scope.ServiceProvider.GetService(typeof(Admin07Dbcontext)) as Admin07Dbcontext;

                var _user = scope.ServiceProvider.GetService(typeof(IResponsitory<Users>)) as EfResponsitory<Users>;

                var _role = scope.ServiceProvider.GetService(typeof(IResponsitory<Roles>)) as EfResponsitory<Roles>;

                db.Database.EnsureCreated();

                var hasUser = db.Users.Any();

                if (!hasUser)
                {
                    var role = new Roles()
                    {
                        RoleName = "名字"
                    };

                    _role.Insert(role);

                    var user = new Users()
                    {
                        UserName = "张三",
                        PassWord = "123456",
                        Roles = role
                    };

                    _user.Insert(user);

                }
            }
        }


    }
}
